<?php
/**
 * Created by PhpStorm.
 * User: admin
 * Date: 2019/11/12
 * Time: 13:51
 */

namespace app\adminapiv3\controller;

use think\facade\Request;
use think\facade\Validate;

class NormalRule extends Base
{
    /**
     * @name 列表
     * @param $host_id
     * @param $group_id
     * @author wx
     * @date 2019/11/12 14:34
     */
    public function list($host_id, $group_id)
    {
        $params['description'] = $this->filterInput('description', 'get');
        $params['status'] = $this->filterInput('status', 'get');
        $params['handle'] = $this->filterInput('handle', 'get');

        $server = new \app\adminapiv3\service\NormalRule();
        $list = $server->toDo('list',[$host_id, $group_id, $params, $this->getLimit()]);

        $this->jsonOutPut($list);
    }

    /**
     * @name 开启
     * @author wx
     * @date 2019/11/12 14:54
     */
    public function open($host_id)
    {
        global $logContent;
        $logContent = '启用官方策略';

        $id = $this->filterInput('id*','post');
        $params['status'] = $this->filterInput('status*', 'post');

        $server = new \app\adminapiv3\service\NormalRule();
        $server->modify($host_id, $id, $params);

        $this->jsonOutPut([],true);

        $this->Async('Wipf', 'WipfRules');
    }

    /**
     * @name 停用
     * @author wx
     * @date 2019/11/12 14:55
     */
    public function close($host_id)
    {
        global $logContent;
        $logContent = '停用官方策略';

        $id = $this->filterInput('id*','post');
        $params['status'] = $this->filterInput('status*', 'post');

        $server = new \app\adminapiv3\service\NormalRule();
        $server->modify($host_id, $id, $params);

        $this->jsonOutPut([],true);

        $this->Async('Wipf', 'WipfRules');
    }

    /**
     * @name 配置
     * @author wx
     * @date 2019/11/12 14:56
     */
    public function setting($host_id)
    {
        global $logContent;
        $logContent = '配置官方策略';

        $id = $this->filterInput('id*','post');
        $params['status'] = $this->filterInput('status*', 'post');
        $params['handle'] = $this->filterInput('handle*', 'post');

        $server = new \app\adminapiv3\service\NormalRule();
        $server->modify($host_id, $id, $params);

        $this->jsonOutPut([],true);

        $this->Async('Wipf', 'WipfRules');
    }


    /**
     * @name: 批量修改处理方式
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2020-09-27 17:12:44
     */
    public function bulkModifyHandle ($host_id)
    {
        global $logContent;
        $logContent = '批量修改安全策略处理方式';
        $input = Request::only(['host_id','rule_id','handle','status','group_id'], 'post');
        $validate = Validate::make([
            'host_id' => 'require|number',
            'rule_id' => 'require|regex:^\d+(,\d+){0,30}$',
            'handle' => 'require|in:0,1,2',
            'group_id' => 'require|number',
            'status' => 'require|in:0,1',
        ]);

        if (!$validate->check($input))
        {
            returnErrJson(422, $validate->getError());
        }

        $server = new \app\adminapiv3\service\NormalRule();
        $server->bulkModifyHandle($input);

        $this->jsonOutPut([],true);

        $this->Async('Wipf', 'WipfRules');
    }

}